package com.isoft.argi.controller;

import com.isoft.argi.pojo.*;
import com.isoft.argi.services.BoardService;
import com.isoft.argi.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/APP")
public class AppController {
    @Autowired
    UserServices userServices ;

    @Autowired
    BoardService boardService ;

    @RequestMapping("login")
    @ResponseBody
    public UserRes login(String N , String P) {
        UserRes ur = new UserRes();
        User u = userServices.userLogin(N, P);
        ur.setErrorcode(0);
        ur.setStatus(u == null ? "fail" : "success");
        ur.setUser(u == null ? new User() : u);
        return ur;
    }

    @RequestMapping("register")
    @ResponseBody
    public JsonResult register(String N , String P, String phone) {
        int r = userServices.userRegister(N , P, phone) ;
        JsonResult result = new JsonResult("0" , r==1 ? "success" : "fail" , r==1 ? userServices.selectUserByName(N) : new User()) ;
        return result ;
    }

    @RequestMapping("checkUser")
    @ResponseBody
    public JsonResult checkUser(String N, String phone) {
        User user = new User();
        int r = userServices.userCheck(N, phone);
        JsonResult result = new JsonResult();
        if(r == -1) {
            result.setErrorcode("-1");
            result.setStatus("fail");
            result.setResults(user);
        } else if(r == 0) {
            result.setErrorcode("0");
            result.setStatus("fail");
            result.setResults(user);
        } else if(r == 2) {
            result.setErrorcode("2");
            result.setStatus("fail");
            result.setResults(user);
        } else {
            result.setErrorcode("1");
            result.setStatus("success");
            result.setResults(userServices.selectUserByName(N));
        }
        return result;
    }

    @RequestMapping("repass")
    @ResponseBody
    public JsonResult repass(String N , String P , String ID) {
        int r = userServices.userEdit(ID , N , P) ;
        JsonResult result = new JsonResult("0" , r==1 ? "success" : "fail" , r==1 ? "ok" : "fail") ;
        return result ;
    }

    @RequestMapping("changePassword")
    @ResponseBody
    public JsonResult changePassword(String N, String P) {
        int r = userServices.changePassword(N, P);
        JsonResult result = new JsonResult("0", r==1? "success" : "fail", r==1 ? userServices.selectUserByName(N) : new User());
        return result;
    }

    @RequestMapping("getsensors")
    @ResponseBody
    public JsonResult getLastData() {
        BoardNum boardNum = boardService.getLastData() ;
        List<DD> list=new ArrayList<DD>();
        String status ;
        if (boardNum != null) {
            DD d1=new DD();
            d1.setSensorName("soilHum");
            d1.setDataUnit("%");
            d1.setData(boardNum.getBhum());

            DD d2=new DD();
            d2.setSensorName("airTemp");
            d2.setDataUnit("℃");
            d2.setData(boardNum.getBtem());

            DD d3=new DD();
            d3.setSensorName("light");
            d3.setDataUnit("klx");
            d3.setData(boardNum.getBlig());

            list.add(d2);
            list.add(d1);
            list.add(d3);

            status = "success" ;
        } else {
            status = "fail" ;
        }

        JsonResult result = new JsonResult("0" , status ,list) ;
        return result ;
    }

    @RequestMapping("getSensorDataByInterval")
    @ResponseBody
    public JsonResult getSensorDataByInterval(String sensorName , String interval , Integer rowNum) {
        if(sensorName == null) {
            return null ;
        }
        String unit = null ;
        String dbSensorName = null ;
        if ("airTemp".equalsIgnoreCase(sensorName)) {
            dbSensorName="btem";
            unit = "℃" ;
        }
        if ("light".equalsIgnoreCase(sensorName)) {
            dbSensorName="blig";
            unit = "klx" ;
        }
        if ("soilHum".equalsIgnoreCase(sensorName)) {
            dbSensorName="bhum";
            unit = "%" ;
        }

        List<Double> list = boardService.getIntervalData(dbSensorName,interval, rowNum) ;
        ArrayList<DD> listResult =new ArrayList<DD>();
        for(Double data : list){
            DD d=new DD();
            d.setSensorName(sensorName);
            d.setDataUnit(unit);
            d.setData(data);
            listResult.add(d);
        }

        String status = "";
        if(null != list && list.size() > 0) {
            status = "success" ;
        } else {
            status = "fail" ;
        }
        JsonResult result = new JsonResult("0" , status ,listResult) ;
        return result ;
    }

}
